package com.rambo.baselib.util;

import com.rambo.baselib.BaseApp;
import com.rambo.baselib.ResourceTable;
import it.neokree.materialnavigationdrawermodule.util.Toast;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class ToastUtil {
    public static void toast(String message) {
        UiExecutor.getInstance().runOnMainThread(new Runnable() {
            @Override
            public void run() {
                runOnMainThread(message);
            }
        });
    }

    private static void runOnMainThread(String message) {
        ToastDialog toast = new ToastDialog(BaseApp.getApp());
        toast.setAlignment(LayoutAlignment.CENTER);
        Text text = new Text(BaseApp.getApp());
        ComponentContainer.LayoutConfig layoutConfig = text.getLayoutConfig();
        layoutConfig.height = DpUtil.dp2px(BaseApp.getApp().getResourceManager(), 48);
        layoutConfig.width = DpUtil.getScreenW(BaseApp.getApp()) - layoutConfig.height;
        text.setLayoutConfig(layoutConfig);
        ShapeElement shapeElement = new ShapeElement(BaseApp.getApp(),
                ResourceTable.Graphic_bg_gray_shape);

        text.setBackground(shapeElement);
        text.setMultipleLine(false);
        text.setTextColor(Color.WHITE);
        text.setTextSize(DpUtil.dp2px(BaseApp.getApp().getResourceManager(), 14));
        toast.setComponent(text);
        text.setText(message != null ? message : "");
        text.setTextAlignment(TextAlignment.CENTER);
        toast.setDuration(3 * 1000);
        toast.show();
    }
}
